<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\SurprisedGoods;
use app\api\library\ExceptionHandle;
use think\Db;
/**
 *  封装的商城接口
 *
 *  Author:  YSX
    Date:    2022-7-20
 */
class Third extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /*
     * 列表
    */
    public function goodsList()
    {
        // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['is_del'] = array('eq',1);
		$where['is_enable'] = array('eq',1);
		
		$limit    = !empty($params['limit']) ? $params['limit'] : 10;
		$page     = !empty($params['page']) ? $params['page'] : 1;
		
        $data['list'] = SurprisedGoods::where($where)
			  ->order('sort desc ,id desc')
			  ->field('id,goods_name,image,sales,price,add_time,state')
			  ->page($page)
			  ->limit($limit)
			  ->select(); 
		foreach($data['list'] as $key => $value){
			$data['list'][$key]['attr'] = Db::name('surprised_goods_attr')->where('surprised_goods_id',$value['id'])->column('attr_name');
		}
		$data['count']    = SurprisedGoods::where($where)->count();
		if ($data) {
			$this->success('获取成功', $data);
		}
		$this->error('请求失败');
    }

    /*
     * 详情
     */
    public function goodsDetail()
    {
        if (!$this->request->isPost()) {
        	return $this->error('提交方式不正确');
        }
		$params = $this->request->post();
		if(!isset($params['goods_id']) || empty($params['goods_id'])){
			return $this->error('参数错误');
		}
		$where['is_del'] = array('eq',1);
		$where['is_enable'] = array('eq',1);
		$data = SurprisedGoods::where('id', $params['goods_id'])
			->where($where)
			->field('id,goods_name,image,carousel_image,sales,price,introduction,add_time,state')
			->find();
		$data['carousel_image'] = \explode(',',$data['carousel_image']);
		$data['attr'] = Db::name('surprised_goods_attr')->where('surprised_goods_id',$params['goods_id'])->field('id,attr_name')->select();
		$this->success('获取成功', $data);
    }
	/*
	 * 加入购物车
	 */
	public function addCart()
	{
	    if (!$this->request->isPost()) {
	    	return $this->error('提交方式不正确');
	    }
		$params = $this->request->post();
		//判断是从哪里过来的
		$type = $params['type'];
		$member_id = $params['member_id'];
		$where['type'] = array('eq',$type);
		$where['member_id'] = array('eq',$member_id);
		$where['surprised_goods_id'] = array('eq',$params['surprised_goods_id']);
		$where['surprised_goods_attr_id'] = array('eq',$params['surprised_goods_attr_id']);
		//判断这个商品属性是否存在
		$isFind = Db::name('surprised_cart')->where($where)->find();
		if($isFind){
			//则数量添加
			$result = Db::name('surprised_cart')->where($where)->setInc('number',$params['number']);
		}else{
			//则添加
			$data['type'] = $type;
			$data['member_id'] = $member_id;
			$data['surprised_goods_id'] = $params['surprised_goods_id'];
			$data['surprised_goods_attr_id'] = $params['surprised_goods_attr_id'];
			$data['number'] = $params['number'];
			$result = Db::name('surprised_cart')->insert($data);
		}
		if ($result) {
			$this->success('加入购物车成功');
		}
		$this->error('加入购物车失败');
	}
	/**
	 * 我的地址
	 *
	 */
	public function myAddress()
	{
	    $page = $this->request->param('page');
	    $rows = $this->request->param('rows');
	    //判断是从哪里过来的
	    $type = $this->request->param('type');
	    $member_id = $this->request->param('member_id');
	    if($page && $rows){
	        $data = Db::name('member_address')->alias('s')
			->join('areas a', 's.province = a.id','left')
			->join('areas b', 's.city = b.id','left')
			->join('areas c', 's.county = c.id','left')
			->field('s.*,a.name as province_name,b.name as city_name,c.name as county_name')
			->where(['s.type'=>$type,'s.member_id'=>$member_id,'s.is_del'=>1])->page($page,$rows)->order(['s.is_default'=>'desc','s.id'=>'desc'])->select();
	    }else{
	        $data = Db::name('member_address')->alias('s')
			->join('areas a', 's.province = a.id','left')
			->join('areas b', 's.city = b.id','left')
			->join('areas c', 's.county = c.id','left')
			->field('s.*,a.name as province_name,b.name as city_name,c.name as county_name')
			->where(['s.type'=>$type,'s.member_id'=>$member_id,'s.is_del'=>1])->order(['s.is_default'=>'desc','s.id'=>'desc'])->select();
	    }
	    $this->success(__('获取成功'), $data);
	}
	
	/**
	 * 新增地址
	 *
	 */
	public function addAddress()
	{
	    $username = $this->request->param('username');
	    $mobile = $this->request->param('mobile');
	    $province = $this->request->param('province');
	    $city = $this->request->param('city');
	    $county = $this->request->param('county');
	    $address = $this->request->param('address');
	    $is_default = $this->request->param('is_default');
	    $type = $this->request->param('type') ? : 1;
	    $id = $this->request->param('id');
	    if (! $username || ! $mobile || ! $province|| ! $city || ! $county|| ! $address|| ! isset($is_default)) {
	        $this->error(__('参数错误'));
	    }
	    //判断是从哪里过来的
		$type = $this->request->param('type');
	    $member_id = $this->request->param('member_id');
	    
	    // $userAddress = new \app\common\model\UserAddress();
	    if($is_default == 1){
	        //修改其它地址为非默认
	        Db::name('member_address')->where(['member_id'=>$member_id])->update(['is_default'=>0]);
	    }
	    $param['member_id'] = $member_id;
	    $param['username'] = $username;
	    $param['mobile'] = $mobile;
	    $param['province'] = $province;
	    $param['city'] = $city;
	    $param['county'] = $county;
	    $param['address'] = $address;
	    $param['is_default'] = $is_default;
	    $param['type'] = $type;
	    $param['createtime'] = $_SERVER['REQUEST_TIME'];
	    if($id){
	        $res = Db::name('member_address')->where(['id'=>$id])->update($param);
			if($res){
				$addressInfo = Db::name('member_address')->alias('s')
					->join('areas a', 's.province = a.id','left')
					->join('areas b', 's.city = b.id','left')
					->join('areas c', 's.county = c.id','left')
					->field('s.*,a.name as province_name,b.name as city_name,c.name as county_name')
					->where(['s.id'=>$id])->find();
			    $this->success(__('提交成功'),$addressInfo);
			}else{
			    $this->error(__('提交失败'));
			}
	    }else{
	        $ids = Db::name('member_address')->insertGetId($param);
			if($ids){
				$addressInfo = Db::name('member_address')->alias('s')
					->join('areas a', 's.province = a.id','left')
					->join('areas b', 's.city = b.id','left')
					->join('areas c', 's.county = c.id','left')
					->field('s.*,a.name as province_name,b.name as city_name,c.name as county_name')
					->where(['s.id'=>$ids])->find();
			    $this->success(__('提交成功'),$addressInfo);
			}else{
			    $this->error(__('提交失败'));
			}
	    }
	    
	}
	
	/**
	 * 设置默认地址
	 *
	 */
	public function setDefaultAddress()
	{
	    $id = $this->request->param('id');
	    if (! $id) {
	        $this->error(__('参数错误'));
	    }
	    //判断是从哪里过来的
	    $type = $this->request->param('type');
	    $member_id = $this->request->param('member_id');
	    //修改其它地址为非默认
	    Db::name('member_address')->where(['type'=>$type,'member_id'=>$member_id])->update(['is_default'=>0]);
	    $res = Db::name('member_address')->where(['id'=>$id])->update(['is_default'=>1]);
	    if($res){
	        $this->success(__('设置成功'));
	    }else{
	        $this->error(__('设置失败'));
	    }
	}
	
	/**
	 * 删除地址
	 *
	 */
	public function deleteAddress()
	{
	    $id = $this->request->param('id');
	    if (! $id) {
	        $this->error(__('参数错误'));
	    }
	    //判断是从哪里过来的
	    $type = $this->request->param('type');
	    $member_id = $this->request->param('member_id');
	    //删除地址
	    $res = Db::name('member_address')->where(['type'=>$type,'id'=>$id,'member_id'=>$member_id])->update(['is_del'=>2]);
	    if($res){
	        $this->success(__('删除成功'));
	    }else{
	        $this->error(__('删除失败'));
	    }
	}
	/**
	 * 地址详情
	 *
	 */
	public function addressDetail()
	{
	    $id = $this->request->param('id');
	    //判断是从哪里过来的
	    $type = $this->request->param('type');
	    $member_id = $this->request->param('member_id');
	    $data = Db::name('member_address')->alias('s')
			->join('areas a', 's.province = a.id','left')
			->join('areas b', 's.city = b.id','left')
			->join('areas c', 's.county = c.id','left')
			->field('s.*,a.name as province_name,b.name as city_name,c.name as county_name')
			->where(['s.type'=>$type,'s.id'=>$id])->find();
			
	    $this->success(__('获取成功'), $data);
	}
	/*
	 * 确认订单接口
	*/
	public function confirm()
	{
	    // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['cart_id'])){
			//从购物车下单
			$data = Db::name('surprised_cart as a')->where('a.id','in',$params['cart_id'])
				->join('surprised_goods x', 'a.surprised_goods_id = x.id')
				->join('surprised_goods_attr o', 'a.surprised_goods_attr_id = o.id')
			    ->field('a.id,a.number,x.goods_name,x.image,x.price,x.stock,o.attr_name')
			    ->order('a.id desc')
			    ->select(); 
			$goods = [];
			foreach($data as $key => $value){
				//判断商品库存是否足够
				if($value['stock'] < $value['number']){
					$goods[] = $value['goods_name'];
				}
				$data[$key]['payMoney'] = $value['price'] * $value['number'];
			}
			//判断商品库存是否足够
			if(count($goods) > 0){
				$tip = implode('、',$goods);
				$this->error($tip.'商品库存不足,请联系管理员上新！');
			}
			if ($data) {
				$this->success('获取成功', $data);
			}
			$this->error('请求失败');
		}else{
			//直接支付去下单
			$where['is_del'] = array('eq',1);
			$where['is_enable'] = array('eq',1);
			$where['id'] = array('eq',$params['surprised_goods_id']);
			
			$goodsInfo = Db::name('surprised_goods')->field('id,goods_name,image,price,stock')->where($where)->find();
			//判断商品库存
			if($goodsInfo['stock'] < $params['number']){
				$this->error('商品库存不足,请联系管理员上新！');
			}
			$goodsInfo['attr_name'] = Db::name('surprised_goods_attr')->where('id',$params['surprised_goods_attr_id'])->value('attr_name');
			$goodsInfo['payMoney'] = $params['number'] * $goodsInfo['price'];
			if ($goodsInfo) {
				$this->success('获取成功', $goodsInfo);
			}
			$this->error('请求失败');
		}
	}
	/*
	 * 下单接口
	*/
	public function confirmOrder()
	{
	    // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		//判断是从哪里过来的
		$type = $this->request->param('type');
		$userid = $this->request->param('member_id');
		if(isset($params['cart_id'])){
			//查找用户地址信息
			$addressInfo = Db::name('member_address')->where('id',$params['address_id'])->find();
			//生成订单号
			$order_no = date('YmdHis', time()) . str_pad(mt_rand(1, 99999), 5, 0, STR_PAD_LEFT) . rand(1000, 99999);
			// \var_dump($pay_money);die;
			//启动事务
			Db::startTrans();
			try {
				$data = [
					'order_no' => $order_no,
					'type' => $type,
					'member_id' => $userid,
					'mobile' => $addressInfo['mobile'],
					'username' => $addressInfo['username'],
					'province' => $addressInfo['province'],
					'city' => $addressInfo['city'],
					'county' => $addressInfo['county'],
					'address' => $addressInfo['address'],
					'housenumber' => $addressInfo['housenumber'],
					'address_id' => $params['address_id'],
					'pay_money' => 0,
					'status' => 1,
					'create_time' => time(),
				];
				$order_id = Db::name('surprised_order')->insertGetId($data);
				//从购物车下单
				$goodsInfo = Db::name('surprised_cart as a')->where('a.id','in',$params['cart_id'])
					->join('surprised_goods x', 'a.surprised_goods_id = x.id')
					->join('surprised_goods_attr o', 'a.surprised_goods_attr_id = o.id')
					->field('a.id,a.surprised_goods_id,a.surprised_goods_attr_id,a.number,x.goods_name,x.image,x.stock,x.price,o.attr_name')
					->order('a.id desc')
					->select(); 
				$goods = [];
				$pay_money = 0;
				foreach($goodsInfo as $key => $value){
					//判断商品库存是否足够
					if($value['stock'] < $value['number']){
						$goods[] = $value['goods_name'];
					}
					$goods_data[$key]['order_id'] = $order_id;
					$goods_data[$key]['goods_id'] = $value['surprised_goods_id'];
					$goods_data[$key]['goods_attr_id'] = $value['surprised_goods_attr_id'];
					$goods_data[$key]['attr_name'] = $value['attr_name'];
					$goods_data[$key]['goods_name'] = $value['goods_name'];
					$goods_data[$key]['image'] = $value['image'];
					$goods_data[$key]['price'] = $value['price'];
					$goods_data[$key]['number'] = $value['number'];
					$goods_data[$key]['create_time'] = time();
					$pay_money += $value['price'] * $value['number'];
				}
				
				//判断商品库存是否足够
				if(count($goods) > 0){
					$tip = implode('、',$goods);
					$res['info'] = $tip.'商品库存不足,请联系管理员上新！';
					$this->error($tip.'商品库存不足,请联系管理员上新！');
				}
				
				Db::name('surprised_order_goods')->insertAll($goods_data);
				//修改支付总价
				Db::name('surprised_order')->where('id',$order_id)->update(['pay_money'=>$pay_money]);
				//删除购物车信息晚点再写
				
				// 提交事务
				Db::commit();
				$res['order_no'] = $order_no;
				$res['pay_money'] = $pay_money;
			}catch (\Exception $e) {
				// \var_dump($e);
			    // 回滚事务
			    Db::rollback();
			}	
			if(isset($res['order_no'])){
				$this->success('下单成功', $res);
			}else{
				if(isset($res['info'])){
					$this->error($res['info']);
				}else{
					$this->error('下单失败');
				}
			}
		}else{
			//查找商品信息
			$goodsInfo = Db::name('surprised_goods')->field('id,goods_name,image,stock,price')->where('id',$params['surprised_goods_id'])->find();
			//判断商品库存
			if($goodsInfo['stock'] < $params['number']){
				$this->error('商品库存不足,请联系管理员上新！');
			}
			//查找用户地址信息
			$addressInfo = Db::name('member_address')->where('id',$params['address_id'])->find();
			$attr_name = Db::name('surprised_goods_attr')->where('id',$params['goods_attr_id'])->value('attr_name');
			//生成订单号
			$order_no = date('YmdHis', time()) . str_pad($params['surprised_goods_id'], 5, 0, STR_PAD_LEFT) . rand(1000, 99999);
			
			//启动事务
			Db::startTrans();
			try {
				// \var_dump($pay_money);die;
			    $data = [
			        'order_no' => $order_no,
			        'member_id' => $userid,
					'type' => $type,
					'mobile' => $addressInfo['mobile'],
					'username' => $addressInfo['username'],
					'province' => $addressInfo['province'],
					'city' => $addressInfo['city'],
					'county' => $addressInfo['county'],
					'address' => $addressInfo['address'],
					'housenumber' => $addressInfo['housenumber'],
			        'address_id' => $params['address_id'],
			        'pay_money' => $goodsInfo['price'] * $params['number'],
			        'status' => 1,
			        'create_time' => time(),
			    ];
			    $order_id = Db::name('surprised_order')->insertGetId($data);
			
				//添加订单商品表
				$goods_data = [
				    'order_id' => $order_id,
				    'goods_id' => $goodsInfo['id'],
				    'goods_attr_id' => $params['goods_attr_id'],
				    'attr_name' => $attr_name,
				    'goods_name' => $goodsInfo['goods_name'],
				    'image' => $goodsInfo['image'],
				    'price' => $goodsInfo['price'],
				    'number'=>$params['number'],
				    'create_time' => time(),
				];
				Db::name('surprised_order_goods')->insert($goods_data);
			    // 提交事务
			    $result = Db::commit();
				$res['order_no'] = $order_no;
				$res['pay_money'] = $data['pay_money'];
			    
			}catch (\Exception $e) {
				// \var_dump($e);
			    // 回滚事务
			    Db::rollback();
			    // $this->error('下单失败');
			}
			if(isset($res['order_no'])){
				$this->success('下单成功', $res);
			}else{
				$this->error('下单失败');
			}
		}
	}
	/*
	 * 我的订单接口
	*/
	public function myOrder(){
	   // $params = $this->params;
	    if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
	    }
	    $params = $this->request->post();
		$userid = $params['member_id'];
	    $where['member_id'] = array('eq',$userid);
	    $where['is_del'] = array('eq',1);
	    if(isset($params['status']) && !empty($params['status'])){
			$where['status'] = array('eq',$params['status']);
			// $where[] = ['coupon_code_class_id', '=' , ];
	    }
		if(isset($params['type']) && !empty($params['type'])){
			$where['type'] = array('eq',$params['type']);
			// $where[] = ['coupon_code_class_id', '=' , ];
		}
	    $list = Db::name('surprised_order')->field('id,order_no,create_time,pay_money,status')->where($where)->order('id desc')->select();
	    if($list){
		    foreach($list as $key => $value){
			    $list[$key]['gooodsInfo'] = Db::name('surprised_order_goods')->field('id,goods_id,goods_attr_id,goods_name,attr_name,image,price,number')->where('order_id',$value['id'])->select();					
		    }
	    }
		$this->success('我的订单', $list);
	} 
	/*
	 * 订单详情
	*/
	public function orderDetail(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['x.is_del'] = array('eq',1);
		if(isset($params['id']) && !empty($params['id'])){
			$where['x.id'] = array('eq',$params['id']);
		}
		if(isset($params['type']) && !empty($params['type'])){
			$where['x.type'] = array('eq',$params['type']);
			// $where[] = ['coupon_code_class_id', '=' , ];
		}
		//查找订单数据
		$info = Db::name('surprised_order')
			->alias('x')
			->join('member_address s', 's.id = x.address_id','left')
			->join('areas a', 's.province = a.id','left')
			->join('areas b', 's.city = b.id','left')
			->join('areas c', 's.county = c.id','left')
			->field('x.id,x.order_no,x.create_time,x.pay_money,x.status,x.address_id,s.*,a.name as province_name,b.name as city_name,c.name as county_name')
			->where($where)->find();
		$info['gooodsInfo'] = Db::name('surprised_order_goods')->field('id,goods_id,goods_attr_id,goods_name,attr_name,image,price,number')->where('order_id',$params['id'])->select();		
		$this->success('订单详情', $info);
	}
	/*
	 * 取消订单
	*/
	public function cancelOrder(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['is_del'] = array('eq',1);
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		if(isset($params['type']) && !empty($params['type'])){
			$where['type'] = array('eq',$params['type']);
			// $where[] = ['coupon_code_class_id', '=' , ];
		}
		//取消订单
		$result = Db::name('surprised_order')->where($where)->update(['status'=>5]);
		if ($result) {
			$this->success('取消成功');
		}
		$this->error('取消失败');
	}
	/*
	 * 删除订单
	*/
	public function delOrder(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		//取消订单
		$result = Db::name('surprised_order')->where($where)->update(['is_del'=>2]);
		if ($result) {
			$this->success('删除成功');
		}
		$this->error('删除失败');
	}
	/*
	 * 确认收货
	*/
	public function confirmReceipt(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		if(isset($params['id']) && !empty($params['id'])){
			$where['id'] = array('eq',$params['id']);
		}
		//把状态改成已完成
		$result = Db::name('surprised_order')->where($where)->update(['status'=>4]);
		if ($result) {
			$this->success('收货成功');
		}
		$this->error('收货失败');
	}
	/**
	 * 微信统一支付
	 */
	public function pay()
	{
	    $order_no = $this->request->post('order_no');
	    if (!$order_no) {
	        $this->error('订单号不能为空');
	    }
	    $order = Db::name('surprised_order')->where('order_no', $order_no)->find();
	    if (!$order) {
	        $this->error('订单不存在');
	    }
	    $user = User::where(['id' => $this->auth->id])->find();
	    if ($user['id'] != $this->auth->id) {
	        $this->error('当前订单不属于当前用户');
	    }
	    $openid = Third::where(['user_id' => $user['id']])->value('openid');
	    if(!$openid) {
	        $this->error('openid 为空不能进行支付');
	    }
	    if ($order['pay_status'] == 2) {
	        $this->error('订单已支付');
	    }
	
		//通知地址  确保外网能正常访问
		$notify_url = 'https://'.$_SERVER['HTTP_HOST'].'/index.php/api/Third/notify';
		//统一下单接口
		$ExceptionHandle = new ExceptionHandle();
		$unifiedorder = $ExceptionHandle->unifiedorder($openid,$order_no,$order['pay_money'],$notify_url,'惊喜商品订单');
		if ($unifiedorder['result_code'] == 'FAIL') {
			$this->error($unifiedorder['err_code_des']);
		}
		$wx_config = Config::get('wx_config');
		$parameters = array(
			'appId' => $wx_config['appid'], //小程序 ID
			'timeStamp' => '' . time() . '', //时间戳
			'nonceStr' => $ExceptionHandle->createNoncestr(), //随机串
			'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
			'signType' => 'MD5'//签名方式
		);
		//签名
		$parameters['paySign'] = $ExceptionHandle->getWxSign($parameters,$wx_config['mch_key']);
		$parameters['order_no'] = $order_no;
		$this->success('获取成功', $parameters);
	}
	
	public function NotifyUrlStatus()
	{
	    $order_no = $this->request->post('out_trade_no');;
		$order =Db::name('surprised_order')->where(['order_no' => $order_no])->find();
		$orderGoods = Db::name('surprised_order_goods')->where(['order_id' => $order['id']])->select();
		$update = [
			'status' => 2,
			'pay_status' => 2,
			'pay_time' => time(),
		];
		$result = Db::name('surprised_order')->where(['id' => $order['id']])->update($update);
		// $result = true;
		if($result){
			foreach($orderGoods as $key => $value){
				//把商品已售数量加1
				SurprisedGoods::where(['id' => $value['goods_id']])->setInc('sales',$value['number']);
			}
			//增加支付记录
			$p_data['user_id'] = $order['member_id'];
			$p_data['order_id'] = $order['id'];
			$p_data['mobile'] = Db::name('member_address')->where(['id' => $order['address_id']])->value('mobile');
			$p_data['money'] = $order['pay_money'];
			$p_data['content'] = '鑫互助支付';
			$p_data['type'] = $order['type'];
			$p_data['add_time'] = $_SERVER['REQUEST_TIME'];
			$p_data['add_date'] = date("Y-m-d H:i:s",$p_data['add_time']);
			$res = Db::name('pay_log')->insert($p_data);
			if($res){
				return true;
			}else{
				return false;
			}
		}
	}
	/*
	 * 列表
	*/
	public function cartInfo()
	{
	    // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		//判断是从哪里过来的
		$type = $params['type'];
		$member_id = $params['member_id'];
		$where['a.member_id'] = array('eq',$member_id);
		$where['a.type'] = array('eq',$type);
		
	    $data = Db::name('surprised_cart as a')->where($where)
			->join('surprised_goods x', 'a.surprised_goods_id = x.id','left')
			->join('surprised_goods_attr o', 'a.surprised_goods_attr_id = o.id','left')
		    ->field('a.id,a.number,x.goods_name,x.image,x.price,o.attr_name')
		    ->order('a.id desc')
		    ->select(); 
		
		$this->success('获取成功', $data);
	}
	/*
	 * 编辑购物车
	 */
	public function editCart()
	{
	    if (!$this->request->isPost()) {
	    	return $this->error('提交方式不正确');
	    }
		$params = $this->request->post();
		$result = Db::name('surprised_cart')->where('id',$params['id'])->setField('number',$params['number']);
		// if ($result) {
		$this->success('编辑成功');
		// }
		// $this->error('编辑失败');
	}
	/*
	 * 删除购物车
	 */
	public function delCart()
	{
	    if (!$this->request->isPost()) {
	    	return $this->error('提交方式不正确');
	    }
		$params = $this->request->post();
		$result = Db::name('surprised_cart')->where('id','in',$params['ids'])->delete();
		if ($result) {
			$this->success('删除成功');
		}
		$this->error('删除失败');
	}
}
